<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%--
  Created by IntelliJ IDEA.
  User: 13415372994
  Date: 2024/1/30
  Time: 14:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <style>
        * {
            margin: 0 auto;
            text-align: center;
        }
    </style>
    <script type="text/javascript" src="${pageContext.request.contextPath}/statics/js/jquery.js"></script>
</head>
<body>
<form action="list" method="get">
    车名：<input type="text" style="text-align: left" name="carName" value="${param.carName}"/>
    车型：
    <select id="carTypeSelect" name="carType">
        <c:forEach var="carType" items="${carTypes}">
            <option value="${carType.tid}">${carType.name}</option>
        </c:forEach>
    </select>
    <input type="submit" value="查询"/>
</form>
<h1>二手车管理系统</h1>
<table border="1">
    <tr>
        <th>车名</th>
        <th>车价(元)</th>
        <th>里程</th>
        <th>购车时间</th>
        <th>车型</th>
        <th>车辆状态</th>
        <th>操作</th>
    </tr>
    <c:forEach var="car" items="${cars}">
        <tr>
            <td>${car.carName}</td>
            <td>${car.carPrice}</td>
            <td>${car.carMileage}</td>
            <td><fmt:formatDate value="${car.carBuyTime}" pattern="yyyy-MM-dd"/></td>
            <td>${car.carType == 1 ? "轿车":"SUV"}</td>
            <td>${car.carStatus == 1 ? "在售":"已售"}</td>
            <td><a href="modify?carId=${car.carId}">修改</a></td>
        </tr>
    </c:forEach>
</table>
<p>
    <a href="list?pageNo=1&carName=${param.carName}&carType=${param.carType}">首页</a>
    <a href="list?pageNo=${pageNo - 1}&carName=${param.carName}&carType=${param.carType}">上一页</a>
    <a href="list?pageNo=${pageNo + 1}&carName=${param.carName}&carType=${param.carType}">下一页</a>
    <a href="list?pageNo=${carPages}&carName=${param.carName}&carType=${param.carType}">末页</a>
    当前显示第${pageNo}页，总页数${carPages}页，共${carCounts}条记录
</p>
</body>
<script>
    $("form").submit(function (){
        var carTypeSelect = document.getElementById('carTypeSelect');
        var paramCarType = new URLSearchParams(window.location.search).get('carType'); // 获取地址栏中的 carType 参数值
        if (paramCarType) { // 如果参数存在
            // 遍历所有选项并设置默认选中的选项
            for (var i = 0; i < carTypeSelect.options.length; i++) {
                if (carTypeSelect.options[i].value === paramCarType) {
                    carTypeSelect.selectedIndex = i; // 设置默认选中的选项
                    break;
                }
            }
        }
    })
</script>
</html>
